package com.lixw.langchain.controller;

import cn.hutool.core.codec.Base64;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @ClassName: ImageModelController
 * @Description:
 * @Author: xuweiLi
 * @Create: 2025/8/24 10:10
 **/
@RestController
public class ImageModelController {
    @Autowired
    private ChatModel chatModel;
    @Value(value = "classpath:static/images/img.png")
    private Resource resource;


    @GetMapping(value = "/image/call")
    public String readImageContent() throws IOException {
        byte[] byteArray = resource.getContentAsByteArray();
        String base64Str = Base64.encode(byteArray);

        UserMessage userMessage = UserMessage.from(
                TextContent.from("从以下的图片中获取来源网站名称，股价走势和7月31号的股价"),
                ImageContent.from(base64Str,"image/jpg")
        );
        ChatResponse chatResponse = chatModel.chat(userMessage);
        String result = chatResponse.aiMessage().text();
        System.out.println(result);
        return result;
    }


}